package gui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

import model.Model;
import model.Pixmap;

import javax.swing.AbstractAction;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JMenuBar;
import javax.swing.JSeparator;

import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.border.CompoundBorder;
import javax.swing.border.EmptyBorder;
import javax.swing.border.EtchedBorder;
import javax.swing.border.TitledBorder;

/**
 * @author Troy Ferrell
 *
 */
public class PicassaGUI extends PicassaFrame
{	
	private static final String PICTURE_FRAME_STR = "Picture Frame";
	//private static final String PICASSO_BG_PATH = "src//resources//picassa.png";
	private static final String PICASSO_ICON_PATH = "src//resources//picassaIcon.png";
	
	private static final int PADDING = 25;
	private static final int PICASSA_HEIGHT = 80;
	private static final int EXPRESSION_HEIGHT = 200;
	private static final int PROPERTIES_WIDTH = 200;
	
	// Model to interact with for system
	private Model myModel;
	
	private ExpressionPanel myExpressionPanel;
	
	// JFrame organizing picassa pictures 
	private PictureFrame myPicFrame;
	
	private PropertiesFrame myPropertiesFrame;
	private PromptFrame myPromptFrame;

	public PicassaGUI(Model newModel)
	{
		//Set JFrame parameters
		setModel(newModel);
		this.setTitle("PICASSA");
		this.setDefaultCloseOperation(EXIT_ON_CLOSE);
		this.setIconImage(new ImageIcon(PICASSO_ICON_PATH).getImage());
		
		createMenuBar();
		createSystemFrames(newModel);
		
		myExpressionPanel = new ExpressionPanel(newModel);
		myExpressionPanel.addExpressionObserver(myPropertiesFrame);
		myExpressionPanel.addExpressionObserver(myPicFrame);
		
		this.getContentPane().add(myExpressionPanel);
		
		setupInitialDimensions();
	}
	
	/**
	 * Create MenuBar for Picassa application
	 */
	public void createMenuBar()
	{
		JMenuBar myMenuBar = new JMenuBar();
		myMenuBar.add(makeFileMenu());
		myMenuBar.add(makeViewMenu());
		this.setJMenuBar(myMenuBar);
	}
	
	/**
	 * Create window components of PICASSA system
	 */
	public void createSystemFrames(Model newModel)
	{
		myPropertiesFrame = new PropertiesFrame(newModel);
		myPicFrame = new PictureFrame(PICTURE_FRAME_STR, newModel);
		myPromptFrame = new PromptFrame(newModel);
	}
	
	/**
	 * Create menu for menubar that deals with files(open,save,etc)
	 * 
	 * @return the File Menu
	 */
	public JMenu makeFileMenu()
	{
		JMenu fileMenu = new JMenu("File");
		fileMenu.add(new AbstractAction("Open")
        {
            public void actionPerformed (ActionEvent e)
            {
            	// TODO: Write code here to open new image
            }
        });
		fileMenu.add(new AbstractAction("Save")
        {
            public void actionPerformed (ActionEvent e)
            {
                  //TODO: Write code here to save image
            }
        });
		fileMenu.add(new JSeparator());
		fileMenu.add(new AbstractAction("Quit")
        {
            public void actionPerformed (ActionEvent e)
            {
                System.exit(0); // end program
            }
        });
		return fileMenu;
	}
	
	/**
	 * Create menu for menubar that deals with view and windows of application
	 * - hide/show windows
	 * - create new picture
	 * - refresh current picture
	 * 
	 * @return the View Menu
	 */
	public JMenu makeViewMenu()
	{
		JMenu viewMenu = new JMenu("View");
		
		JCheckBoxMenuItem pictureViewMenuItem = new JCheckBoxMenuItem("Picture Frame");
		pictureViewMenuItem.setSelected(true);
		viewMenu.add(pictureViewMenuItem);
		pictureViewMenuItem.addItemListener(new ItemListener(){
			public void itemStateChanged(ItemEvent e)
			{
				myPicFrame.setVisible(e.getStateChange() == ItemEvent.SELECTED);
			}
		});
		
		JCheckBoxMenuItem propertiesViewMenuItem = new JCheckBoxMenuItem("Properties");
		propertiesViewMenuItem.setSelected(true);
		viewMenu.add(propertiesViewMenuItem);
		propertiesViewMenuItem.addItemListener(new ItemListener(){
			public void itemStateChanged(ItemEvent e)
			{
				myPropertiesFrame.setVisible(e.getStateChange() == ItemEvent.SELECTED);
			}
		});
		
		JCheckBoxMenuItem promptViewMenuItem = new JCheckBoxMenuItem("Prompt");
		promptViewMenuItem.setSelected(true);
		viewMenu.add(promptViewMenuItem);
		promptViewMenuItem.addItemListener(new ItemListener(){
			public void itemStateChanged(ItemEvent e)
			{
				myPromptFrame.setVisible(e.getStateChange() == ItemEvent.SELECTED);
			}
		});
		
		viewMenu.add(new JSeparator());
		viewMenu.add(new AbstractAction("New Picture")
        {
            public void actionPerformed (ActionEvent e)
            {
            	myPicFrame.addNewTab(myPicFrame.NEW_IMAGE_STR);
            }
        });
		viewMenu.add(new AbstractAction("Refresh Picture")
        {
            public void actionPerformed (ActionEvent e)
            {
            	myPicFrame.repaint();
            }
        });

		return viewMenu;
	}
  
	/**
	 * Set up system to initial dimensions based on current machine screen size
	 */
	private void setupInitialDimensions()
	{
		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
		this.setSize(screenSize.width, PICASSA_HEIGHT);
		
		myPicFrame.setSize(Pixmap.DEFAULT_SIZE);
		myPicFrame.setLocation(screenSize.width/6, screenSize.height/6);
		
		myPromptFrame.setSize(screenSize.width, EXPRESSION_HEIGHT);
		myPromptFrame.setLocation(0 ,screenSize.height - EXPRESSION_HEIGHT);
		
		myPropertiesFrame.setSize(PROPERTIES_WIDTH, screenSize.height - EXPRESSION_HEIGHT - PICASSA_HEIGHT - 2*PADDING);
		myPropertiesFrame.setLocation(screenSize.width - PROPERTIES_WIDTH, PICASSA_HEIGHT + PADDING);
	}
    
    /**
     * Make sure to set picture frame visible when application 
     */
    @Override
    public void setVisible(boolean visible)
    {
    	super.setVisible(visible);
    	myPicFrame.setVisible(visible);
    	myPropertiesFrame.setVisible(visible);
    	myPromptFrame.setVisible(visible);	
    }
   
}
